package com.xilihui.xlh.component.recyclerview;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;


import com.xilihui.xlh.R;
import com.xilihui.xlh.core.util.ScreenUtil;

import java.util.List;


public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {

    private static final int NORMAL = 0x100;

    private static final int THE_END = 0x200;

    private boolean isTheEnd;

    protected Context context;

    private List<T> list;

    private T t;

    // 动画相关
    private int lastAnimatedPosition = -1;

    private boolean animationsLocked = false;

    private
    @LayoutRes
    int endLayoutRes;

    public BaseAdapter(Context context, List<T> list) {
        this(context, list, R.layout.recycler_the_end);
    }

    public BaseAdapter(Context context, List<T> list, @LayoutRes int endLayoutRes) {
        this.context = context;
        this.list = list;
        this.endLayoutRes = endLayoutRes;
    }

    /**
     * 实例化范型类
     *
     * @param clazz
     */
    public void setClassInstance(Class<T> clazz) {
        try {
            t = clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 在最后位置加一个item
     *
     * @param isTheEnd
     */
    public void setIsTheEnd(boolean isTheEnd) {
        this.isTheEnd = isTheEnd;
        if (this.isTheEnd && list != null) {
            list.add(t);
        }
        notifyDataSetChanged();
    }

    public boolean isTheEnd(int position) {
        if (isTheEnd) {
            if (position == getItemCount() - 1) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    protected abstract
    @LayoutRes
    int setLayoutId();

    protected abstract void getView(BaseViewHolder holder, T entity, int position);

    @Override
    public int getItemViewType(int position) {

        if (isTheEnd) {
            if (position == getItemCount() - 1) {
                return THE_END;
            } else {
                return NORMAL;
            }
        } else {
            return NORMAL;
        }
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == NORMAL) {
            View view = LayoutInflater.from(context).inflate(setLayoutId(), parent, false);
            return new BaseViewHolder(view);
        } else if (viewType == THE_END) {
            View view = LayoutInflater.from(context).inflate(endLayoutRes, parent, false);
            return new FooterViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        if (isTheEnd && position == getItemCount() - 1) {
            // 最后一个item跳过
            runEnterAnimation(holder.itemView, position);
            return;
        }
        getView(holder, list.get(position), position);
        runEnterAnimation(holder.itemView, position);
    }

    @Override
    public final int getItemCount() {
        return list == null ? 0 : list.size();
    }

    // RecyclerView进入动画
    private void runEnterAnimation(View view, int position) {

        if (animationsLocked)
            return;//animationsLocked是布尔类型变量，一开始为false，确保仅屏幕一开始能够显示的item项才开启动画

        if (position > lastAnimatedPosition) {//lastAnimatedPosition是int类型变量，一开始为-1，这两行代码确保了recycleview滚动式回收利用视图时不会出现不连续的效果
            lastAnimatedPosition = position;
            view.setTranslationY(ScreenUtil.getScreenHeight(context));//相对于原始位置下方
            view.setAlpha(0.f);//完全透明
            //每个item项两个动画，从透明到不透明，从下方移动到原来的位置
            //并且根据item的位置设置延迟的时间，达到一个接着一个的效果
//            boolean delayEnterAnimation = true;
            view.animate()
                    .translationY(0)
                    .alpha(1.f)
                    .setStartDelay(50 * position)//根据item的位置设置延迟时间，达到依次动画一个接一个进行的效果
                    .setInterpolator(new DecelerateInterpolator(2f))//设置动画效果为在动画开始的地方快然后慢
                    .setDuration(500)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    notifyDataSetChanged();
                                }
                            }, 150);
                            animationsLocked = true;//确保仅屏幕一开始能够显示的item项才开启动画，也就是说屏幕下方还没有显示的item项滑动时是没有动画效果
                        }
                    })
                    .start();
        }
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public boolean isAnimationsLocked() {
        return animationsLocked;
    }

    public void setAnimationsLocked(boolean animationsLocked) {
        this.animationsLocked = animationsLocked;
    }
    
}
